
#include <stdio.h>
#include <graphics.h>
#include <math.h>


double checkDet(double M[200][200],int rk)
{
    double det;
    det =0;
    if (rk == 2)
    {
       det += M[0][0] * M[1][1] - M[0][1] * M[1][0];
    }
    else
    {
        double minor[200][200];
        int k = 0; 
        int i,i2,k2;
        for (i = 0; i <= rk - 1; i++) 
        {            
            for (i2 = 0; i2 <= rk - 1; i2++) 
            {
                if (i2 != i)
                {
                    for (k2 = 1; k2 <= rk - 1; k2++) 
                    {
                        if (k2 != k)
                        {
                            if (i2 < i)
                            {
                                minor[i2][k2 - 1] = M[i2][k2];
                            }
                            else if (i2 > i)
                            {
                                minor[i2 - 1][k2 - 1] = M[i2][k2];
                            }
                        }
                    }
                }
            }
            
            if(i%2==1)
            {
                det -= M[i][k]*checkDet(minor, rk-1);
            }
            else
            {
                det += M[i][k]*checkDet(minor, rk-1);
            }
        }
    }
    return det;
}

double * solvematrix(double** M, double * N,int n)
{
    double * X = new double[n];
    
    int i,k,i2;
    double koef,sum=0;
    double A[n][n];
    double B[n];
    
    for (k = 0; k < n; k++) 
    {
        for (i = 0; i < n; i++) 
        {            
            A[i][k]=*(((double*)M)+i*200+k);           
        }
        B[k]=N[k];
    } 
    
    for (i = 0; i <= n - 2; i++) 
    {        
        for (k = i+1; k <= n - 1; k++) 
        {
                koef=A[i][i]/A[i][k];
                for (i2 = i; i2 <= n - 1; i2++) 
                {
                    A[i2][k] = A[i2][k] * koef - A[i2][i];
                }
                B[k] = B[k] * koef - B[i];
        }
    }   
    
            
    for (k = n - 1; k >= 0; k--) 
    {
        sum = 0;
        for (i = n - 1; i > k; i--) 
        {
            sum += A[i][k] * (*(X+i));
        }
        *(X+k) = (B[k] - sum) / A[k][k];
    }
    return X;
}

int main() 
{
    int n;
    n = 4; 
            
    double A[200][200];
    double B[200];
    double * X = new double[n];


    double norm;
    int step=0;

    A[0][0] = 2;
    A[0][1] = 1;
    A[0][2] = 1;
    A[0][3] = 1;

    A[1][0] = 1;
    A[1][1] = 4;
    A[1][2] = 1;
    A[1][3] = 1;

    A[2][0] = 1;
    A[2][1] = 1;
    A[2][2] = 6;
    A[2][3] = 1;

    A[3][0] = 1;
    A[3][1] = 1;
    A[3][2] = 1;
    A[3][3] = 8;

    B[0] = 4;
    B[1] = 6;
    B[2] = 8;
    B[3] = 10;
    
    double d;
    d = checkDet(A, n);
    double e = 0.001;

    printf("det = %f\n",d);


    int k,i;
    k=0;
    
    
    for (k = 0; k <= n - 1; k++) 
    {
        for (i = 0; i <= n - 1; i++)
        {
            printf("%f ",A[i][k]);
        }
        printf("\n");
    }
    printf("\n\n");
        
    
    X = solvematrix((double**)A,(double*)B,n);
    
    
    for (int i = 0; i <= n - 1; i++)
    {
            printf("x%d = %f; ",i,*(X+i));
    }
    printf("\n");
    
    n=1;
    while(true==true)
    {
        getchar();
        n++;
        double * X1 = new double[n];
        printf("n = %d\n",n);
        
        
        for (k = 0; k <= n - 1; k++) 
        {
            for (i = 0; i <= n - 1; i++) 
            {
                if(i==k)
                {
                    
                    A[i][k] = 1+e;
                }
                else if(i<k)
                {
                    A[i][k] = e;
                }
                else if(i>k)
                {
                    A[i][k] = 1-e;
                }
            }
            if(k==n-1)
            {
                B[k] = 1+e;
            }
            else
            {
                B[k] = -1;
            }
        }   
    
        for (k = 0; k <= n - 1; k++) 
        {
            for (i = 0; i <= n - 1; i++) 
            {
                printf("%f ",A[i][k]);
            }
            printf("| %f\n",B[k]);
        }       
        
        X1 = solvematrix((double**)A,(double*)B,n);
    
        for (int i = 0; i <= n - 1; i++)
        {
            printf("x%d = %.15f; ",i,*(X1+i));
            norm += X1[i]*X1[i];
        }
        printf("\n");
        
        norm = pow(norm,0.5)/n;
        printf("norm = %.15f\n",norm);       
        
        fillellipse(101+step*5,200-(int)(norm*100),1,1);       
        
        step++;
        delete X1;
        if(step >=16)
        {
         break;   
        }
    }
    
    
    getchar();
    closegraph();
	return 0;
}



        
